#include "graphletsutil.h"
#include <QDomDocument>
#include <QDomElement>
#include <QSvgRenderer>
#include <QImage>
#include <QPainter>
GraphletsUtil::GraphletsUtil() {}

Graphlets GraphletsUtil::loadFromXml(QString xmlCode){
    QDomDocument svgDoc;
    svgDoc.setContent(xmlCode);
    QDomElement svgEl=svgDoc.documentElement();
    QString name = svgEl.attribute("name");
    QString guid = svgEl.attribute("guid");
    QString type = svgEl.attribute("type");
    Graphlets graphlets;
    graphlets.m_gName = name;
    graphlets.m_gGuid = guid;
    graphlets.m_gType = type;
    graphlets.m_gCode = xmlCode;
    //处理变量
    QDomNodeList nodeList = svgEl.elementsByTagName("defs");
    //qDebug() << "GraphletsUtil::loadFromXml:defs size->" << nodeList.size();
    QList<GraphletsVar> varList;
    if(nodeList.size()>0){
        QDomNode defNode = nodeList.at(0);
        QDomNodeList varNodeList = defNode.childNodes();
        for(int i=0;i<varNodeList.size();i++){
            QDomNode varNode = varNodeList.at(i);
            QDomElement varEl = varNode.toElement();
            if(varNode.nodeName()=="variable"){
                GraphletsVar gVar;
                gVar.m_id = varEl.attribute("id");
                gVar.m_viewName = varEl.attribute("viewName");
                gVar.m_group = varEl.attribute("group");
                gVar.m_varType = varEl.attribute("varType");
                gVar.m_dataType = varEl.attribute("dataType");
                gVar.m_initialValue = varEl.attribute("initialValue");
                varList.append(gVar);
            }
        }
    }
    graphlets.m_gVars = varList;
    return graphlets;
}

QString GraphletsUtil::encodeSvgCode(QString svgCodeRaw){
    char* svgCodeEnc = crypt(svgCodeRaw.toStdString().c_str(),1);
    QString res(svgCodeEnc);
    //return res;
    return svgCodeRaw;
}//加密svg代码

QString GraphletsUtil::decodeSvgCode(QString svgCodeEncrypted){
    char* svgCodeEnc = crypt(svgCodeEncrypted.toStdString().c_str(),-1);
    QString res(svgCodeEnc);
    return res;
}//解密svg代码

QString GraphletsUtil::genThumbnail(QString svgCode,QString svgFileName){
    QSvgRenderer renderer(svgCode.toUtf8());
    QSize thumbnailSize(20,20);
    QString thumbUrl=svgFileName;
    QImage thumbnail(thumbnailSize,QImage::Format_ARGB32);
    thumbnail.fill(Qt::transparent);
    QPainter painter(&thumbnail);
    QRect thumbRect(QPoint(0,0),thumbnailSize);
    QRectF thumbRectF(QPointF(0,0),thumbnailSize);
    painter.setViewport(thumbRect);
    renderer.render(&painter,thumbRectF);
    thumbnail.save(thumbUrl);
    return thumbUrl;
}


/**
 * @brief GraphletsUtil::crypt
 * @param text
 * @param cryptType 加密或解密  1 加密  -1解密
 * @return
 */
char* GraphletsUtil::crypt(const char* text,int cryptType){
    int textLen = strlen(text);
    int shift = textLen/shiftSeeds*cryptType;
    char* encrypted=new char[textLen];
    for (int i = 0; i < textLen; ++i) {
        if ('a' <= text[i] && text[i] <= 'z') {
            int tchar = (text[i] - 'a' + shift) % 26 + 'a';
            encrypted[i] = static_cast<char>(tchar);
        } else if ('A' <= text[i] && text[i] <= 'Z') {
            encrypted[i] = static_cast<char>((text[i] - 'A' + shift) % 26 + 'A');
        }
    }
    return encrypted;
}
